package org.tsp.android.GUI_Menus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.tsp.android.GUI_Menus.R;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class GUI_MenusActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Boolean text_bold = false;
	private Boolean text_italic = false;
	
	private Handler mHandler = new Handler();
	
	private Boolean slideshow_started = false;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
	
	@Override
	public void onResume()
	{
		super.onResume();

		if ( slideshow_started )
			mHandler.postDelayed(mUpdateTimeTask, 3000);
	}
	
	@Override 
	public void onPause()
	{
		super.onPause();
		
		mHandler.removeCallbacks(mUpdateTimeTask);
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		
	}
	public void showSecondActivity(View v)
	{
		Intent intent = new Intent(GUI_MenusActivity.this,Another_Activity.class);
        startActivity(intent);
	}
	
    public void loadImageClick(View v)
    {
    	loadImage("/sdcard/img.jpg");
    }
    
    private void loadImage(String img_name)
    {
    	Bitmap aBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
      	 ImageView aImageView = (ImageView) findViewById(R.id.img_view);
      	 aImageView.setImageBitmap(aBitmap); 
    }
    
    public void startSlidesowClick(View v)
    {
    	slideshow_started = true;
    	mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 3000);
    }
    
    private void loadImageFromStream(InputStream img_stream)
    {
    	final ImageView img =(ImageView)findViewById(R.id.img_view);
    	final Bitmap bmp = BitmapFactory.decodeStream(img_stream);
		this.runOnUiThread(new Runnable(){

			public void run() {
				img.setImageBitmap(bmp);
			}
			
		});
    }
    
    private void loadImageAssets(String image)
    {	
    	try {
      		 InputStream is = getResources().getAssets().open(image); 
      		 loadImageFromStream(is);
      		 is.close();
      	 } catch (IOException e) {
      		 // TODO Auto-generated catch block
      		 e.printStackTrace();
      	 }
    }
    int image_index=0;
    private Runnable mUpdateTimeTask = new Runnable() {
    	   public void run() {
    		   String[] files;
   			mHandler.postDelayed(mUpdateTimeTask, 3000);
   			try {
   			 AssetManager assetManager = getAssets();
   			 files = assetManager.list("Images");
   			 if ( files.length == 0 )
   				 return;
   			 loadImageAssets("Images"+File.separator + files[image_index++]);
   			 if ( image_index == files.length )
   				 image_index = 0;
   			} catch (IOException e) {
   				e.printStackTrace();
   			}    
    	   }
    	};
    
       
    @Override 
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inf = getMenuInflater();
    	inf.inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch (item.getItemId())
    	{
    		case R.id.menu_exit:
    			this.finish();
    			return true;
    		case R.id.menu_second:
    			this.showSecondActivity(null);
    			return true;
    		case R.id.bold:	
    				item.setChecked(!item.isChecked());
    				text_bold=item.isChecked();
    				changeStyleAllButton();
    				return true;
    		case R.id.italic:
    			item.setChecked(!item.isChecked());
    			text_italic=item.isChecked();
    			changeStyleAllButton();
				return true;
    			// To Complete
    			// Change the checked state of the menu item
    			// and update the text style

    		default: 
    			return super.onOptionsItemSelected(item);
    	}
    }
    
    private void changeStyleAllButton()
    {
    	int idStyle;
    	if (text_bold ==true)
		{
    		if (text_italic==true)
    		{
    			idStyle=R.style.BoldItalicButtonFont;
    		}else{
    			idStyle=R.style.BoldButtonFont;
    		}
		}else{
			if(text_italic ==true)
			{
				idStyle=R.style.ItalicButtonFont;
			}else{
				idStyle=R.style.ButtonFont;
			}
		}
    	Button bto = (Button)findViewById(R.id.btn_load_image);
		bto.setTextAppearance(this, idStyle);
		bto = (Button)findViewById(R.id.btn_start_slideshow);
		bto.setTextAppearance(this, idStyle);
		bto = (Button)findViewById(R.id.button1);
		bto.setTextAppearance(this, idStyle);
    }


}